<template>
  <el-dialog title="咨询工单处理" :visible.sync="visible" append-to-body width="800px">
    <el-form :model="form" ref="form">
      <el-form-item label="协助内容">
        {{ dataInfo.content }}
      </el-form-item>
      <el-form-item label="是否满足">
        <el-radio v-model="form.disposeState" label="1">满足</el-radio>
        <el-radio v-model="form.disposeState" label="2">不满足</el-radio>
      </el-form-item>
      <el-form-item label="不满足原因" prop="reason" v-if="form.disposeState == 2" :rules="[{
        required: true, message: '请选择不满足原因', trigger: 'change'
      }]">
        <XmSelect v-model="form.reason" placeholder="不满足原因" :options="reasonList"></XmSelect>
      </el-form-item>
      <el-form-item label="备注/不满足原因" prop="resultContent" :rules="[{
        required: isContentRequired, message: '请填写备注/不满足原因', trigger: 'blur'
      }]">
        <el-input type="textarea" v-model="form.resultContent" rows="5" autocomplete="off"></el-input>
      </el-form-item>
      
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submit">提 交</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  updateAsk,
} from "@/api/workorder/consultHappy";
const reasonList = [{
  value: '人手不足',
  label: '人手不足',
},{
  value: '距离较远',
  label: '距离较远',
},{
  value: '时间不满足',
  label: '时间不满足',
},{
  value: '其他',
  label: '其他',
}]

export default {
  data() {
    return {
      reasonList,
      visible: false,
      dataInfo: {},
      form: {
        disposeState: '1',
        reason: '',
        resultContent: ''
      },
    };
  },
  computed: {
    isContentRequired() {
      return ['时间不满足', '其他'].includes(this.form.reason)
    }
  },
  methods: {
    show({data}) {
      this.visible = true
      this.dataInfo = data
      this.clearForm()
    },
    clearForm() {
      this.form = {
        disposeState: '1',
        reason: '',
        resultContent: ''
      }
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          updateAsk(this.getParams()).then(res => {
            if(res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '处理成功',
              })
              this.visible = false
              this.$emit('fetch')
            }
          })
        }
      })
    },
    getParams() {
      const content = this.form.disposeState == 2 ?
        `${this.form.reason == '其他' ? '' : `【${this.form.reason}】`}${this.form.resultContent}` :
        this.form.resultContent
      return {
        id: this.dataInfo.id,
        disposeState: this.form.disposeState,
        resultContent: content,
      }
    }
  }
};
</script>
<style lang="scss" scoped>

</style>